package com.xiaoi.user.example;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.xiaoi.ibotos.android.sdk.APIs;
import com.xiaoi.ibotos.android.sdk.APIsListener;
import com.xiaoi.ibotos.android.sdk.iBotSignature;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
    Button start,start_UI;
    TextView content;

    class GUIShowInfoRunnable implements Runnable
    {
        private MainActivity _activity;
        private String _text;

        public GUIShowInfoRunnable(MainActivity activity, String text)
        {
            _activity = activity;
            _text = text;
        }

        @Override
        public void run()
        {
            showDebugInfoOnlyInMainThread(_text);
        }
    }

    public void showDebugInfo(String newinfo)
    {
        this.runOnUiThread(new GUIShowInfoRunnable(this, newinfo));
    }

    public void showDebugInfoOnlyInMainThread(String newInfo)
    {
        TextView textView = (TextView)findViewById(R.id.xiaoi_content);
        textView.setText(textView.getText().toString() + (textView.getText().toString().isEmpty() ? "" : "\n") + newInfo);

        final Layout layout = textView.getLayout();
        if(layout != null){
            int scrollDelta = layout.getLineBottom(textView.getLineCount() - 1)
                    - textView.getScrollY() - textView.getHeight();
            if(scrollDelta > 0)
                textView.scrollBy(0, scrollDelta);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        content = (TextView)findViewById(R.id.xiaoi_content);
        content.setTextColor(Color.BLUE);
        start = (Button)findViewById(R.id.xiaoi_start);
        start_UI = (Button)findViewById(R.id.xiaoi_start_UI);
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                APIs.setVADActive(true);
                APIs.setListener(new APIsListener() {
                    @Override
                    public void onEvent(APIs.Event event) {
                        switch(event.getEventID())
                        {

                            case EVENT_DEVICE_RECORD_STOP:
                            case EVENT_DEVICE_RECORD_START:
                            {
                                showDebugInfo("一一一" + event.getValue());

                                break;
                            }

                            case EVENT_DEVICE_PLAY_START:
                            case EVENT_DEVICE_PLAY_STOP:

                            {
                                showDebugInfo("一一一" + event.getValue());

                                break;
                            }

                            case EVENT_ANSWER_ONLINE_SERVICE_START:
                            case EVENT_REG_ONLINE_SERVICE_START:
                            case EVENT_TTS_ONLINE_SERVICE_START:
                            case EVENT_TTS_ONLINE_SERVICE_OK_FINISHED:
                            {

                                showDebugInfo("一一一" + event.getValue());

                                break;
                            }

                            case EVENT_ANSWER_ONLINE_SERVICE_OK_FINISHED:
                            {
                                showDebugInfo("一一一" + event.getValue());

                                break;
                            }
                            case EVENT_REG_ONLINE_SERVICE_OK_FINISHED:
                            {
                                showDebugInfo("一一一" + event.getValue());

                                doAsk(event.getValue());

                                break;
                            }

                            case EVENT_TTS_ONLINE_SERVICE_FAILED_FINISHED:
                            case EVENT_ANSWER_ONLINE_SERVICE_FAILED_FINISHED:
                            case EVENT_REG_ONLINE_SERVICE_FAILED_FINISHED:
                            {
                                showDebugInfo("一一一" + event.getValue());

                                break;
                            }
                        }
                    }

                    @Override
                    public void onError(APIs.ErrorDescription errorDescription) {
                        showDebugInfo("一一一"+
                                errorDescription.what());
                    }

                    @Override
                    public void onVAD(byte[] bytes) {
                        APIs.stopRecord();

                        doRecognize(bytes, APIs.AudioFormat.AUDIO_FORMAT_PCM_16k16Bits);
                    }
                });
                doAsk("你是谁");
//                doTTS("你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好");
            }
        });
        start_UI.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                APIs.setVADActive(true);
                APIs.setListener(new APIsListener() {
                    @Override
                    public void onEvent(APIs.Event event) {
                        switch(event.getEventID()) {
                            case EVENT_ANSWER_ONLINE_SERVICE_START: {
                                //ask开始
                                showDebugInfo("ask开始:");
                                break;
                            }
                            case EVENT_ANSWER_ONLINE_SERVICE_FAILED_FINISHED: {
                                //ask失败
                                showDebugInfo("ask失败");
                                break;
                            }
                            case EVENT_ANSWER_ONLINE_SERVICE_OK_FINISHED: {
                                //ask结束
                                showDebugInfo("ask"+event.getValue());
//                        APIs.setListener(null);
                                break;
                            }
                        }
                    }

                    @Override
                    public void onError(APIs.ErrorDescription errorDescription) {
                        showDebugInfo("现版本:"+
                                errorDescription.what());
                    }

                    @Override
                    public void onVAD(byte[] bytes) {
                        APIs.stopRecord();
                        doRecognize(bytes, APIs.AudioFormat.AUDIO_FORMAT_PCM_16k16Bits);
                    }
                });
                doAsk("你好");
            }
        });
    }
    protected String _key = "Bear1dj6N424431dj6N";
    protected String _sec = "9d06crMEVDMg88v9q42W";
    protected void doTTS(String tts)
    {
        iBotSignature sign = new iBotSignature(_key, _sec, "/synth.do");
        HashMap<String, String> params = new HashMap<>();
        params.put("tts", tts);
        params.put("url", "http://vcloud.xiaoi.com/synth.do");
        params.put("userId", "007");
        params.put("platform", "android");
        params.put("timeout", "5");

        int sessionID = APIs.doTTS(sign, params);

     //   showDebugInfo("doTTS sessionID:" + sessionID);
    }

    protected void doAsk(String ask)
    {
        iBotSignature sign = new iBotSignature("XRg2TM9Ad1tT", "g1PLtSFBadd3xNJYqulU",  "/ask.do");
        HashMap<String, String> params = new HashMap<>();

        params.put("question", ask);
        params.put("url", "http://nlp.xiaoi.com/ask.do");
        params.put("userId", "007");
        params.put("platform", "android");
        params.put("timeout", "5");

        int sessionID = APIs.doAnswer(sign, params);

       // showDebugInfo("doAnswer sessionID:" + sessionID);
    }

    protected void doRecognize(byte[] audio_data, APIs.AudioFormat format)
    {
        iBotSignature sign = new iBotSignature(_key, _sec, "/recog");
        HashMap<String, String> params = new HashMap<>();

        params.put("url", "http://vcloud.xiaoi.com/recog");
        params.put("userId", "007");
        params.put("platform", "android");
        params.put("timeout", "5");

        int sessionID = APIs.doRecognize(sign, params, audio_data, format);

        //showDebugInfo("doRecognize sessionID:" + sessionID);
    }
}
